define([
     "dojo/_base/declare",
     "gistech/widgets/baseWidget/BaseWidget",
     "gistech/widgets/normalDialog/NormalDialog",
     "dojo/text!./templates/fileUpload.html",
     "../../../app/UIManager",
     "dojo/_base/lang",
     "dojo/topic",
     "dojo/dom-style",
     "dojo/dom-construct",
     "dojo/on",
     "dojo/_base/connect",
     ],function(declare,BaseWidget,NormalDialog,template,UIManager,lang,Topic,style,domConstruct,on,eventconnect){
	    return declare([NormalDialog],{
	    	templateString:template,
	    	attachmentForWho:null,
	    	ownerId:null,
	    	constructor:function(args){
	    		this.inherited(arguments);
	    		this.attachmentForWho=args.who;
	    		this.ownerId=args.ownerId;//uuid
	    	},
	    	postCreate:function(){
                this.inherited(arguments);
            },
            startup:function(){
                this.inherited(arguments);
                this.init();
            },
    		init:function(){
    			debugger
               $(lang.hitch(this,function(){
                   //初始化
            	   var uploader =WebUploader.create({
               		    swf: '../client/gistech/lib/fileUpload/Uploader.swf',
               		    server: '../client/backgroundManagement/AttachmentController/upload/',
               		    pick: {
               		    	id:"#picker",
               		    	innerHTML :"选择文件",
               		    	multiple:false
               		    },
               		    resize: false,
               		    accept:{
               		    	extensions: 'jpg,jpeg,bmp,png,pdf,PDF',
               		    }
               		}); 
            	   //监听文件队列
            	   uploader.on( 'fileQueued', function(file){
            		   debugger
            		    $temp=$("#tempAtta").clone(true);
            		    $temp.attr("id",file.id);
            		    $temp.find("#name").text(file.name);
            		    file.downFlag=$temp.find("input[name='down']:checked").val();
            		    $temp.find("#progress-bar").append($('<div class="progress progress-striped active">' +'<div class="progress-bar" role="progressbar" style="width: 100%">' +'</div>' +'</div>'))
            		    $temp.find("#status").text("等待");
            		    $(".tbody").append($temp);
            		    $temp.show();
            		    $temp.find("[name='down']").change(function(){
            		    	debugger
            		    	if($(this).is(":checked") ==true){
            		    		file.downFlag="1"	
            		    	}else{
            		    		file.downFlag="0"
            		    	}
            		    });
            		    if(this.getFiles("inited").length>0){
             			   $("#ctlBtn").attr("disabled",false);
             		    }
            		    });
            	   
            	   uploader.on('fileDequeued',function(){
            		   if(this.getFiles("inited").length==0){
            			   $("#ctlBtn").attr("disabled",true);
            		   }
            	   });
       		       $("#deleteBtn").click(function(e){
          		       $(e.currentTarget.parentElement.parentElement).remove();
          		       uploader.removeFile(e.currentTarget.parentElement.parentElement.id,true);
          	       })
          	       uploader.on('beforeFileQueued',function(file){
          	    	   filetypeArray=["image/png","image/bmp","application/pdf"];
          	    	   if(filetypeArray.indexOf(file.type)==-1){
          	    		   layer.msg("只支持图片及pdf",{icon:2})
          	    	   }
          	       });
          	       //进度条
            	   uploader.on( 'uploadProgress', function( file, percentage ) {
            		   debugger
            		    var $tr = $('#'+file.id ),
            		    $percent = $tr.find('.progress .progress-bar');
            		    $tr.find('#status').text('上传中');
            		    $percent.css( 'width', percentage * 100 + '%' );
            		});
       		       $("#ctlBtn").click(lang.hitch(this,function(){
       		    	  debugger
       		    	  var obj=new Object();
       		    	  obj.userId=ioc.map.userInfoData.USERID;
       		    	  obj.ownerType=this.attachmentForWho;
       		    	  obj.ownerId=this.ownerId;
       		    	  uploader.options.formData =obj;
       		    	  console.log(obj);
       		    	  debugger
       		    	  dojo.forEach(uploader.getFiles("inited"),function(item,index){
       		    		uploader.upload(item);
       		    	  })
       		       })),
       		       uploader.on('uploadBeforeSend',function(block,data){
       		    	   debugger
       		    	   var file = block.file;
       		    	   var fileName=encodeURIComponent(file.name);
       		    	   var downFlag=file.downFlag;
       		    	   data.downFlag=downFlag;
       		    	   data.fileName=fileName;
       		       }),
            	   //成功回调
            	    uploader.on( 'uploadSuccess', function( file ) {
            		    $( '#'+file.id ).find('#status').text('已上传');
            		    $( '#'+file.id ).find("#deleteBtn").attr("disabled","true");
            		});
                    //失败回调
            		uploader.on('uploadError', function(file,reason) {
            			layer.msg(reason,{icon:2});
            		    $( '#'+file.id ).find('#status').text('上传出错');
            		});
                    //无论如何回调
            		uploader.on( 'uploadComplete', function( file ) {
            		    $( '#'+file.id ).find('.progress').fadeOut();
            		});
               }))
    		}
	    });
     })